package com.example.demo.service;

import com.example.demo.mapper.TorrentCommentMapper;
import com.example.demo.mapper.TorrentMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.Torrent;
import com.example.demo.model.TorrentComment;
import com.example.demo.model.User;
import com.example.demo.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class TorrentCommentService {
    private final TorrentCommentMapper commentMapper;
    private final TorrentMapper torrentMapper;
    private final UserMapper userMapper;
    private final JwtUtils jwtUtils;

    @Autowired
    public TorrentCommentService(TorrentCommentMapper commentMapper,
                                 TorrentMapper torrentMapper,
                                 UserMapper userMapper,
                                 JwtUtils jwtUtils) {
        this.commentMapper = commentMapper;
        this.torrentMapper = torrentMapper;
        this.userMapper = userMapper;
        this.jwtUtils = jwtUtils;
    }

    @Transactional
    public TorrentComment createComment(Long seedId, Long userId, String content, String token) {
        // 验证 token
        String username;
        try {
            username = jwtUtils.getUsernameFromToken(token.substring(7));
        } catch (Exception e) {
            log.error("令牌解析失败", e);
            throw new IllegalArgumentException("令牌解析失败");
        }

        // 验证用户
        User user = userMapper.getUserById(userId);
        if (user == null || !user.getUsername().equals(username)) {
            log.error("用户验证失败 - ID: {}, 用户名: {}", userId, username);
            throw new IllegalArgumentException("用户验证失败");
        }

        // 验证种子
        Torrent torrent = torrentMapper.findById(seedId);
        if (torrent == null) {
            log.error("种子不存在 - ID: {}", seedId);
            throw new IllegalArgumentException("种子不存在");
        }

        // 创建评论
        TorrentComment comment = new TorrentComment();
        comment.setSeedId(seedId);
        comment.setUserId(userId);
        comment.setUsername(user.getUsername());
        comment.setContent(content);
        comment.setTimestamp(LocalDateTime.now());

        commentMapper.insertComment(comment);
        log.info("发表评论成功 - 种子ID: {}, 用户ID: {}, 评论ID: {}", seedId, userId, comment.getId());
        return comment;
    }

    public List<TorrentComment> getComments(Long seedId, Long userId) {
        // 验证种子
        Torrent torrent = torrentMapper.findById(seedId);
        if (torrent == null) {
            log.error("种子不存在 - ID: {}", seedId);
            throw new IllegalArgumentException("种子不存在");
        }

        List<TorrentComment> comments = commentMapper.findBySeedIdOrderByTimestampDesc(seedId);
        log.info("获取评论列表成功 - 种子ID: {}, 数量: {}", seedId, comments.size());
        return comments;
    }
}